<?php
include_once('/var/www/html/new/com.inc.php');

write_logs1(json_encode($_REQUEST));

if(!isset($_SESSION['sid'])) exit("<script>alert('登陆过期');parent.window.location='../dlogin_n.html';</script>");

$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$did=$_REQUEST["did"];
$act=$_REQUEST["act"];
if($act=="c"){//供应商给下级分销商修改授信额度
	
	$money=$_REQUEST["cmoney"];
    if($money<=0){
        echo "<script>alert('授信额度需为正值');</script>";
        exit;
    }
	$meno = $_REQUEST['meno'];
	$_b=simplexml_load_string($s->PFT_Member_Fund($did,2,$_SESSION['sid']));
	$now=$_b->Rec[0]->UUbasecredit/100;
	if(($res=$s->PFT_Member_Fund_Modify((int)$did,(int)$_SESSION['memberID'],(abs($money-$now)*100),($money<$now?1:0),2,(int)$_SESSION['sid'],11,3,'', $meno))==100){
		$sel="select dname from pft_member where id=".$did." limit 1";
		$GLOBALS['le']->query($sel);
		$GLOBALS['le']->fetch_assoc();
		$ins="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",daction='将".$GLOBALS['le']->f("dname")."授信额度配置为".$money."元',rectime=now()";
		$GLOBALS['le']->query($ins);
		echo "<script>alert('还款成功!');window.parent.location=window.parent.location.href;</script>";
	}
}
elseif($act=="t"){//供应商给下级分销商设置的 线下还款
	$money=$_REQUEST["tmoney"];
	$_b=simplexml_load_string($s->PFT_Member_Fund($did,1,$_SESSION['memberID']));
	$now=$_b->Rec[0]->UUkmoney/100;
	// if($money>0){
	/*	if($now>=0 || $money>($now*-1)){
			exit("<script>alert('还款超过钱款!');</script>");
		}*/
    if($money>0){
        $daction = 0;
    }else{
        $daction = 1;
    }
	$meno = $_REQUEST['meno'];
    $money = abs($money);
		if(($res=$s->PFT_Member_Fund_Modify((int)$did,(int)$_SESSION['memberID'],($money*100),$daction,1,(int)$_SESSION['sid'],4,2, '', $meno))==100){
			$sel="select dname from pft_member where id=".$did." limit 1";
			$GLOBALS['le']->query($sel);
			$GLOBALS['le']->fetch_assoc();
			$ins="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",daction='为".$GLOBALS['le']->f("dname")."还款".$money."元',rectime=now()";
			$GLOBALS['le']->query($ins);
			echo "<script>alert('还款成功!');window.parent.location=window.parent.location.href;</script>";
		}
		else{
			echo "<script>alert('".$res."');</script>";
		}
	// }
	// else{
		// echo "<script>alert('还款金额不正确');</script>";
	// }
}
elseif($act=="r"){// 预存/还款

	$money = $_REQUEST["money"];// 预存/还款金额
	if($money<0) exit("<script>alert('还款金额不正确');</script>");
		
	// 预存/还款账户金额判断
	$simxml_o = simplexml_load_string($s->PFT_Member_Fund($_SESSION['sid'],0));
	$nowMoney = $simxml_o->Rec[0]->UUamoney/100;
	if($money>$nowMoney) exit("<script>alert('您当前账户余额资金不足,\"预存/还款\"失败!');</script>");
	
	
	// 减去自己的账户余额
	$sid = (int)$_SESSION['sid'];
	$memberID = (int)$_SESSION['memberID'];
	$money_f = abs($money)*100;
	$res = $s->PFT_Member_Fund_Modify($sid, $memberID, $money_f, 1, 0, '', 3, 0);
	if($res==100){
		$res = $s->PFT_Member_Fund_Modify($sid, $memberID, $money_f, 0, 1, $did, 4, 2);
		if($res==100){

			$ins="insert pft_d_operation_rec set fid=$memberID,sid=$sid,daction='预存/还款$money元',rectime=now()";
			$GLOBALS['le']->query($ins);
			
			$res = $s->PFT_Member_Fund_Modify($did, $memberID, $money_f, 0, 0, '', 3, 0);
			if($res==100){// 把钱给供应商加上
				write_logs("分销商[$sid]预存/还款成功,对应供应商[$did]");
				echo "<script>alert('预存/还款成功!');parent.window.location=window.parent.location.href;</script>";
			}else{
				write_logs("[预存/还款]分销商$sid,供应商未加账户余额[$did]",'setFund_error.log');
			}
		}else{// 自己扣钱了可用供应商余额没有增加，日志
			write_logs("[预存/还款]分销商$sid未加上可供应余额,供应商[$did]",'setFund_error.log');
			echo "<script>alert('".$res."');</script>";
		}
	}else{
		echo "<script>alert('".$res."');</script>";
	}
	


	
}
elseif($act=="z"){//管理员设置的 线下还款
	$money=$_REQUEST["tmoney"];
	if($_SESSION['memberID']!=1 && $_SESSION['dtype']!=7){
		echo "<script>alert('禁止使用该功能!');</script>";
		exit;
	}
	if(($res=$s->PFT_Member_Fund_Modify((int)$did,(int)$_SESSION['memberID'],(abs($money)*100),($money>0?0:1),0,"",3,0))==100){
		$sel="select dname from pft_member where id=".$did." limit 1";
		$GLOBALS['le']->query($sel);
		$GLOBALS['le']->fetch_assoc();
		$ins="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",daction='为".$GLOBALS['le']->f("dname")."线下充值".$money."元',rectime=now()";
		$GLOBALS['le']->query($ins);
		echo "<script>alert('充值成功!');parent.window.location=window.parent.location.href;</script>";
		exit;
	}
	else{
		echo "<script>alert('充值出错:".$res."');</script>";
		exit;
	}
}
elseif($act=="m"){
	
	if(!isset($_SESSION['vcode']) || $_SESSION['vcode']!=$_REQUEST['vcode']) exit('<script>alert("验证码不正确");</script>');
	$money=$_REQUEST["money"];
	$account=$_REQUEST["account"];
    //转账目标
	$sel="select id,dname from pft_member where account='".$account."' limit 1";//echo $sel;
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();
	$id=$GLOBALS['le']->f("id");
	$dname=$GLOBALS['le']->f("dname");
	if(!$id){
		echo "<script>alert('目标账号不存在!');</script>";
		exit;
	}
	$_money=simplexml_load_string($s->PFT_Member_Fund($_SESSION['sid'],0));
	$_money=$_money->Rec[0]->UUamoney/100;
	if($_money<$money){
		echo "<script>alert('账户余额不足!');</script>";
		exit;
	}
    //发起转账人 如果是员工 取主帐号
    if($_SESSION['dtype']==6) {
        $sql = "select dname from pft_member where id={$_SESSION ['sid']} limit 1";
        $GLOBALS['le']->query($sql);
        $GLOBALS['le']->fetch_assoc();
        $dname_maker = $GLOBALS['le']->f('dname');
    }else{
        $dname_maker = $_SESSION['dname'];
    }
	if(($res=$s->PFT_Member_Fund_Modify((int)$_SESSION['sid'],(int)$_SESSION['memberID'],(abs($money)*100),($money>0?1:0),0,"",3,0,"",('向'.$dname."(".$account.')转账'.$money.'元')))==100){
		if(($res=$s->PFT_Member_Fund_Modify((int)$id,(int)$_SESSION['memberID'],(abs($money)*100),($money>0?0:1),0,"",3,0,"",('从'.$dname_maker."(".$_SESSION['saccount'].')收到转账'.$money.'元')))==100){
			$ins="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",daction='向".$dname."转账".$money."元',rectime=now()";
			$GLOBALS['le']->query($ins);
			echo "<script>alert('转账成功!');</script>";
			exit;
		}
		else{
			echo "<script>alert('转账出错:".$res."');</script>";
			exit;
		}
	}
}

function write_logs($content, $file='setFund_success.log'){
	$file = '/var/www/html/new/d/logs/'.$file;
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$content."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}



function write_logs1($txt,$file="/var/www/html/new/d/api/logs/data_111.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}